package com.hdax.dm.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.entity.base.Image;
import com.hdax.dm.entity.item.Item;
import com.hdax.dm.entity.item.ItemType;
import com.hdax.dm.item.dto.HotItemDto;
import com.hdax.dm.item.dto.IndexNavDto;
import com.hdax.dm.item.feign.ImageFeign;
import com.hdax.dm.item.mappers.ItemTypeMapper;
import com.hdax.dm.item.service.IndexNavService;
import com.hdax.dm.item.service.ItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class IndexNavServiceImpl extends ServiceImpl<ItemTypeMapper, ItemType> implements IndexNavService {

    @Autowired
    private ItemService itemService;
    @Autowired
    private ImageFeign imageFeign;

    @Override
    public List<IndexNavDto> nav() {
        //一级分类
        QueryWrapper<ItemType> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("level",1L);
        queryWrapper1.eq("parent",0L);
        List<ItemType> firstTypes = list(queryWrapper1);
        List<IndexNavDto> dtos = new ArrayList<>();
        firstTypes.forEach(itemType1 -> {
            //二级分类
            QueryWrapper<ItemType> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("level",2L);
            queryWrapper2.eq("parent",itemType1.getId());
            List<ItemType> secondTypes = list(queryWrapper2);
            List<IndexNavDto> children = new ArrayList<>();
            secondTypes.forEach(itemType2->{
                children.add(IndexNavDto.builder()
                        .id(itemType2.getId())
                        .parent(itemType2.getParent())
                        .level(itemType2.getLevel())
                        .itemType(itemType2.getItemType())
                        .aliasName(itemType2.getAliasName())
                        .build());

            });
            //热门商品
            QueryWrapper<Item> itemQueryWrapper1=new QueryWrapper<>();//条件
            itemQueryWrapper1.eq("itemType1Id",itemType1.getId());
            itemQueryWrapper1.eq("isBanner",1L);
            itemQueryWrapper1.eq("isRecommend",1L);
            List<Item> items1=itemService.page(new Page<Item>(1,5),itemQueryWrapper1).getRecords();//getRecords()是page集合变为list集合

            //根据一级分类id查询热门商品
            QueryWrapper<Item> itemQueryWrapper = new QueryWrapper<>();
            itemQueryWrapper.eq("isBanner",1L);
            itemQueryWrapper.eq("isRecommend",1L);
            itemQueryWrapper.eq("itemType1Id",itemType1.getId());
            List<Item> items = itemService.page(new Page<Item>(1,5),itemQueryWrapper).getRecords();
            List<HotItemDto> hotItems = new ArrayList<>();
            items.forEach(item->{
                HotItemDto hotItem = new HotItemDto();
                BeanUtils.copyProperties(item,hotItem);
                //TODO 每个商品的图片
                Image image = imageFeign.image(item.getId(),2L,1L);
                hotItem.setImgUrl(image == null || image.getImgUrl() == null ?"":image.getImgUrl());
                hotItems.add(hotItem);
            });
            dtos.add(IndexNavDto.builder()
                    .id(itemType1.getId())
                    .parent(itemType1.getParent())
                    .level(itemType1.getLevel())
                    .itemType(itemType1.getItemType())
                    .aliasName(itemType1.getAliasName())
                    .children(children)
                    .hotItems(hotItems)
                    .build());

        });


        return dtos;
    }
}
